package com.game.springbootstart.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.springbootstart.annotation.AutoFill;
import com.game.springbootstart.config.QueryPageParam;
import com.game.springbootstart.config.Result;
import com.game.springbootstart.entity.Category;
import com.game.springbootstart.enumeration.OperationType;
import com.game.springbootstart.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 游戏分类 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-16
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService iCategoryService;
    @GetMapping("/list")
    public Result list() {
        return Result.success(iCategoryService.list());
    }
    @AutoFill(OperationType.INSERT)
    @PostMapping("/save")
    public Result save(@RequestBody Category category) {
        category.setStatus(1);
        return Result.success(iCategoryService.save(category));
    }
    @AutoFill(OperationType.UPDATE)
    @PostMapping("/mod")
    public Result mod(@RequestBody Category category) {
        return Result.success(iCategoryService.updateById(category));
    }
    @PostMapping("/saveOrMod")
    public Result saveOrMod(@RequestBody Category category) {
        return Result.success(iCategoryService.saveOrUpdate(category));
    }
    @GetMapping("/delete")
    public Result delete(Integer id) {
        return Result.success(iCategoryService.removeById(id));
    }
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        Page<Category> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Category::getName,name);
        IPage<Category> result = iCategoryService.page(page, queryWrapper);
        return Result.success(result.getRecords(), result.getTotal());
    }
    @AutoFill(OperationType.UPDATE)
    @PostMapping("/status")
    public Result update(@RequestBody Category category) {
        int status = category.getStatus();
        if (status == 1) {
            category.setStatus(0);
        } else {
            category.setStatus(1);
        }
        return Result.success(iCategoryService.updateById(category));
    }

}
